﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
using System.Net;
using System.Net.NetworkInformation;
using System.Xml;


namespace mediaFunction
{
    public class sabnzbdAccess
    {
        string serverIP;
        string serverPort;
        string serverAPIkey;

        public sabnzbdAccess(string input0, string input1, string input2)
        {
            serverIP = input0;
            serverPort = input1;
            serverAPIkey = input2;
        }

        /*
         * dump out xml datasheet from sabnzbd
         */
        public XmlTextReader xmlDump()
        {
            string fullPath = "http://" + serverIP + ":" + serverPort + "/sabnzbd/api?mode=queue&output=xml&apikey=" + serverAPIkey;
            return new XmlTextReader(fullPath);
        }

        /*
         * sabnzbd pause method,
         * default is 3 minutes
         */
        public void sabnzbdPause(int minutes)
        {
            string interval = System.Convert.ToString(minutes);        
            HttpWebRequest httpHandle = (HttpWebRequest)WebRequest.Create("http://" + serverIP + ":" + serverPort 
                                                                        + "/sabnzbd/api?mode=config&name=set_pause&value=" + interval
                                                                        + "&apikey=" + serverAPIkey);

            HttpWebResponse httpResponse = (HttpWebResponse)httpHandle.GetResponse();
            return;
        }

        /*
         * delete sabnzbd history
         */
        public void sabnzbdClrHistory()
        {        
            HttpWebRequest httpHandle = (HttpWebRequest)WebRequest.Create("http://" + serverIP + ":" + serverPort
                                                                        + "/sabnzbd/api?mode=history&name=delete&value=all"
                                                                        + "&apikey=" + serverAPIkey);
            HttpWebResponse httpResponse = (HttpWebResponse)httpHandle.GetResponse();
            return;
        }
    }

    public class xmlHandler
    {
        /*
         * this function returns false if node cannot be found
         * if node is found, the text value of the node is returned in string nodeValue
         */
        public bool findValue(XmlTextReader xmlData, string nodeType, ref string nodeValue)
        {
            bool evalFinished = false;
            nodeValue = null;


            /*
             * there is a corner case where the server might've died for some reason and did not
             * start sabnzbd.  In this case, there is no xml to parse.
             * try to catch an xml exception here, and move on as if nothing has happened.
             */
            try{
                xmlData.Read();
            }
            catch (Exception e){
                return false;
            }

            do
            {
                switch (xmlData.NodeType)
                {
                    case XmlNodeType.Element:
                        if (xmlData.Name == nodeType)
                        {
                            Console.WriteLine("Found the {0} node", nodeType);
                            xmlData.Read();
                            nodeValue = xmlData.Value;
                            evalFinished = true;
                        }
                        break;
                }
            } while (xmlData.Read() && !evalFinished);
            /*
            while (xmlData.Read() && !evalFinished)
            {
                switch (xmlData.NodeType)
                {
                    case XmlNodeType.Element:
                        if (xmlData.Name == nodeType)
                        {
                            Console.WriteLine("Found the {0} node", nodeType);
                            xmlData.Read();
                            nodeValue = xmlData.Value;
                            evalFinished = true;
                        }
                        break;
                }
            }
             */
            return evalFinished;
        }
    }
}
